package com.fpost.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.fpost.constants.ActionConstants;
import com.fpost.domain.ArticleVersion;
import com.fpost.form.ArticleVersionPresentationForm;
import com.fpost.form.dto.ArticleVersionDTO;
import com.fpost.manager.ArticleVersionManager;
import com.fpost.util.ActionUtils;
import com.fpost.util.CommonUtils;

public class ArticleVersionPresentationAction extends Action {
	
	private static Logger log = Logger.getLogger(ArticleVersionPresentationAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//set current path in session
		CommonUtils.setCurrentPathInSession(request, mapping);
		
		String forward = ActionConstants.SUCCESS;

		try {
			ArticleVersionPresentationForm presentationForm = (ArticleVersionPresentationForm) form;

			String articleVersionIdStr = (String) request.getParameter("articleVersionId");
			if (articleVersionIdStr != null) {
				long articleVersionId = Long.parseLong(articleVersionIdStr);
				ArticleVersion articleVersion = new ArticleVersionManager().getByArticleVersionId(articleVersionId);
				presentationForm.setArticleVersionDTO(new ArticleVersionDTO(articleVersion));	
				forward = ActionConstants.SUCCESS;
			} else {
				ActionUtils.setErrorMessage(request, "error.article.version.not.found");
				forward = ActionConstants.FAILURE;
			}
		} catch (NumberFormatException e) {
			log.error(e.getMessage());
			ActionUtils.setErrorMessage(request, "error.article.version.not.found");
			forward = ActionConstants.FAILURE;
		} catch (Exception e) {
			log.error(e.getMessage());
			forward = ActionConstants.SYSTEM_FAILURE;
		}

		return mapping.findForward(forward);
	}
	
}
